package com.kandinfo.base.common.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.jdbc.datasource.DataSourceUtils;

import com.kandinfo.base.common.enumeration.EErrorCode;
import com.kandinfo.base.common.enums.EDataSource;
import com.kandinfo.base.common.exception.BaseException;

public class DBUtil {

	private DataSource ds ;
	private Connection conn = null;
	private PreparedStatement st = null;
	private PreparedStatement st1 = null;
	private ResultSet rs=null;

	public DBUtil(int provinceId){
			String dataSourceName=EDataSource.getDataSourceByProvinceId(provinceId);
			ds = (DataSource) ApplicationContextUtil.getApplicationContext().getBean(dataSourceName);
			conn =  DataSourceUtils.getConnection(ds);
	}
	
	public  Connection getConnection() {
		return conn;
	}

	public PreparedStatement getPreparedStatement(String sql)  throws SQLException {
		
			 st = conn.prepareStatement(sql) ;
			 return st ;
		
	}
	
	public Integer exeUpdate(String sql, String[] args)  throws SQLException {
		
			 int id=-1;
				st = conn.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
				if (args != null)
					for (int i = 0; i < args.length; i++){
						st.setString(i + 1, args[i]);
					}
				st.executeUpdate();
				ResultSet rs = st.getGeneratedKeys();
				if(rs.next()){
					id = rs.getInt(1);
				}
			return id;
		
	}
	
	public Integer executeBatch(){
		int id=-1;
        try {
            if(st1!=null)
            	st1.executeBatch();
            ResultSet rs = st1.getGeneratedKeys();
			if(rs.next()){
				id = rs.getInt(1);
			}
        } catch (SQLException e) {
            throw new BaseException(EErrorCode.SQL_EXCEPTION);
        }
        return id;      
	}
	
	public void exeUpdate(String sql,Object[] args) throws SQLException{
	        if(st1==null)
            st1 = conn.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
            if (args != null)
                for (int i = 0; i < args.length; i++){
                    st1.setObject(i + 1, args[i]);
                }
            st1.addBatch();
	}
	
	public void exeUpdate2(String sql,String[] args) throws SQLException{
        if(st1==null)
        st1 = conn.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
        if (args != null)
            for (int i = 0; i < args.length; i++){
                st1.setObject(i + 1, args[i]);
            }
        st1.addBatch();
   }
	
	public ResultSet exeQuery(String sql, String[] args)  throws SQLException {

			 st=conn.prepareStatement(sql);
				int index=1;
				if (args != null)
					for (int i = 0; i < args.length; i++){
						if(args[i]!=null && !args[i].equals("")){
							st.setString(index++, args[i]);
						}
					}
				rs=st.executeQuery();
				return rs;
	}
	
	public ResultSet exeQuery(String sql, Object[] args)  throws SQLException {

        st=conn.prepareStatement(sql);
           int index=1;
           if (args != null)
               for (int i = 0; i < args.length; i++){
                   if(args[i]!=null && !args[i].equals("")){
                       st.setObject(index++, args[i]);
                   }
               }
           rs=st.executeQuery();
           return rs;
	}

	
	public  void releaseConnection() {
		try{
			if(rs!=null) rs.close();
			if(st!=null) st.close();
			if(st1!=null) st1.close();
			DataSourceUtils.releaseConnection(conn, ds);
		 }catch(SQLException e){
		    	throw new BaseException(EErrorCode.SQL_EXCEPTION);
		 }
	}
	
}
